import {observable, action} from 'mobx'
import {WeaTableNew,WeaForm} from 'comsMobx'
const {TableStore} = WeaTableNew
import * as API_LIST from '../../apis/customReport/customReport'
import {ConstantStatus,Permission} from '../../public/FnaConstant'
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

/**
 * list页面Store
 */
export class CustomeListStore {

	constructor(){
	}

	/** 列表store */
    title = getLabel(20412,'自定义报表');
    @observable dataKey = "";
    @observable tableStore = new TableStore();

    @observable searchForm = new WeaForm();
    @observable searchCondition = [];
    @observable showSearchAd = false;

    @observable loading = false;

    /** 路由 **/
    router = {};

    //编辑Modal传入的数据
    @observable editData = {};

    //基础搜索的参数
    @observable nameQuery = '';

    @observable hasRight;//权限的标记 4无权限，跳转
    
    @observable accountColumns = [];
    @observable accountDataSource = [];
    @observable accountVislble = false;

    setAccountVislble = (value) => {
        this.accountVislble = value;
    }

    resetAccountData = () => {
        this.accountColumns = "";
        this.accountDataSource = new TableStore();
        this.accountVislble = false;
    }

    //列表数据
    initDatas = (value) => {
        this.initAdSearchData();
        this.doSearch(value,false);
    }

    //初始化高级搜索
    initAdSearchData = (value) => {
        API_LIST.getAdSearchData(value).then(data => {
            let {status} = data;
            if(status == ConstantStatus.FNA_RESULT_SUCCESS){
                this.hasRight = Permission.FNA_HASRIGHT;
                const { conditions } = data;
                this.searchCondition = conditions;
                this.searchForm = new WeaForm();
                this.searchForm.initFormFields(conditions);
            } else if(status == ConstantStatus.FNA_RESULT_HAS_NO_RIGHT){//没有权限
                this.hasRight = Permission.FNA_NORIGHT;
            }
        });
    }

    //搜索
    doSearch = (value = {},flag = true) => {
        this.loading = true;
        API_LIST.getList(value).then( data => {
            let {status} = data;
            if(status == ConstantStatus.FNA_RESULT_SUCCESS){
                this.hasRight = Permission.FNA_HASRIGHT;
                if(flag){//回到第一页
                    this.tableStore.getDatas(data.datas,1);
                }else{
                    this.tableStore.getDatas(data.datas);
                }
                this.dataKey = data.datas;
            } else if(status == ConstantStatus.FNA_RESULT_HAS_NO_RIGHT){//没有权限
                this.hasRight = Permission.FNA_NORIGHT;
            }    
            this.loading = false;
        });
    }

    //获取账套列表
    getAccountList = (value,callback,openCallBack) => {
        this.loading = true;
        API_LIST.getAccountList(value).then( data => {
            let {status,size,columns,dataSource} = data;
            if(status == ConstantStatus.FNA_RESULT_SUCCESS){
                this.accountColumns = columns;
                this.accountDataSource = dataSource;

                if(size > 1){
                    callback && callback();
                } else {
                    openCallBack({accountId:dataSource[0].accountId});
                }
            } else if(status == ConstantStatus.FNA_RESULT_HAS_NO_RIGHT){//没有权限
                this.hasRight = Permission.FNA_NORIGHT;
            }    
            this.loading = false;
        });
    }

    //标题
    setTitle = (title) => {
        this.title = title;
    }

    //保存编辑的Record
    setEditData = (value) => {
        this.editData = value;
    }

    //保存基础搜索的参数
    setNameQuery = (value) => {
        this.nameQuery = value;
    }

    //重置高级搜索表单
    clearAdvanceForm = () => {
        this.searchForm.resetConditionValue();
    }

    //设置高级搜索的显示
    setShowSearchAd = (value) => {
        this.showSearchAd = value;
    }
}